package cn.jhz.learn.blog.test.impl;

import cn.jhz.learn.blog.component.RequestHolder;
import cn.jhz.learn.blog.service.core.open.GuestbookService;
import cn.jhz.learn.blog.dao.guestbook.BlogGuestbookMapper;
import cn.jhz.learn.blog.po.guestbook.BlogGuestbook;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Optional;

@Validated
@Service("guestbookService")
public class GuestbookServiceImpl implements GuestbookService {
    private final BlogGuestbookMapper guestbookMapper;
    private final BlogBloggerMapper bloggerMapper;

//    @Autowired
    public GuestbookServiceImpl(BlogGuestbookMapper guestbookMapper, BlogBloggerMapper bloggerMapper) {
        this.guestbookMapper = guestbookMapper;
        this.bloggerMapper = bloggerMapper;
    }

    @Override
    public Optional<BlogGuestbook> publishComment(@NotNull @Valid BlogGuestbook newComment) {
        BlogBlogger blogger = checkBloggerExist(newComment.getBlogId());
        checkBloggerVisibility(blogger);
        checkContent(newComment.getContent());
        newComment.setFloorNumber(bloggerMapper.MaxFloorNumberByPrimaryKey(blogger.getId()));
        newComment.setStatus((byte) 1);

        guestbookMapper.insertSelective(newComment);

        return Optional.of(newComment);
    }

    @Override
    public Optional<Integer> removeComment(@NotNull Integer commentId) {
        BlogGuestbook comment = checkExist(commentId);
        if(!comment.getBlogId().equals(RequestHolder.getCurrentUser().getId()))
            throw ExceptionConstant.insufficientAccess.get();

        guestbookMapper.updateStatusByPrimaryKey(commentId, (byte) 0);
        return Optional.of(commentId);
    }

    @Override
    public Optional<BlogGuestbook> modifyComment(BlogGuestbook comment) {
        BlogGuestbook oldComment = checkExist(comment.getId());
        checkBloggerVisibility(checkBloggerExist(oldComment.getBlogId()));
        checkContent(comment.getContent());

        oldComment.setContent(comment.getContent());
        oldComment.setVisibility(comment.getVisibility());

        return Optional.of(oldComment);
    }


    public BlogGuestbook checkExist(Integer commentId){
        //TODO:有效性检查
        BlogGuestbook blogGuestbook = guestbookMapper.selectByPrimaryKey(commentId);
        if(blogGuestbook == null)
            throw ExceptionConstant.paramGuestbookIsNotExist.get();
        else if(!blogGuestbook.getRaterId().equals(RequestHolder.getCurrentUser().getId()))
            throw ExceptionConstant.insufficientAccess.get();
        return blogGuestbook;
    }
    private BlogBlogger checkBloggerExist(Integer bloggerId){
        //TODO:有效性检查
        BlogBlogger blogger = bloggerMapper.selectByPrimaryKey(bloggerId);
        if(blogger == null)
            throw ExceptionConstant.paramBloggerIsNotExist.get();
        else
            return blogger;
    }
    private void checkContent(String content){
        //TODO:内容检查
    }
    private void checkBloggerVisibility(BlogBlogger blogger){
        //TODO:可见性检查
        //TODO:其他信息检查
    }
}
